<style>
    #febs-obj {
        padding: 20px 25px 25px 0;
    }
    #febs-obj .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid layui-anim febs-anim" id="febs-obj" lay-title="业务数据上报">
    <div class="layui-inline">
        <label class="layui-form-label">上传日期</label>
        <div class="layui-input-inline">
            <input type="text" name="date" id="febs-form-group-date" lay-verify="date"
                   placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <form class="layui-form" action="" lay-filter="obj-add-form" id="items_form">
        <table class="layui-table" id="items_table">
            <colgroup>
                <col width="150">
                <col width="200">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
            </colgroup>
            <thead>
            <tr>
                <th>单位名称</th>
                <th>主项</th>
                <th>子项</th>
                <th>事项类别</th>
                <th>业务办理名称</th>
                <th>咨询业务量</th>
                <th>邮寄业务量</th>
                <th>线上业务量</th>
                <th>线下业务量-大厅</th>
                <th>线下业务量-分厅</th>
            </tr>
            </thead>
            <tbody id="items_table_tbody">
            </tbody>
        </table>
        <div class="layui-card">

            <button type="button" class="layui-btn" id="submit" lay-submit="" style="margin-left: 40%;">数据上报</button>
        </div>
        <!--<div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="obj-add-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>-->
    </form>
</div>

<script>
    layui.use(['febs', 'form', 'formSelects', 'validate', 'treeSelect', 'laydate'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            formSelects = layui.formSelects,
            laydate = layui.laydate,
            treeSelect = layui.treeSelect,
            form = layui.form,
            $view = $('#obj-add'),
            validate = layui.validate;

        form.verify(validate);
        form.render();
        laydate.render({
            elem: '#febs-form-group-date',
            done:function(value,date){//value, date, endDate点击日期、清空、现在、确定均会触发。回调返回三个参数，分别代表：生成的值、日期时间对象、结束的日期时间对象
                initTable();
            }
        });
        formSelects.render();

        console.info("--d4444556d--");


        initTable();
        $("#submit").click(function(){
            if($("#febs-form-group-date").val()==''){
                febs.alert.success('请选择日期');
                return false;
            }
            /*febs.post(ctx + 'hallUpData', data.field, function () {
                layer.closeAll();
                febs.alert.success('新增成功');
                $('#febs-obj').find('#query').click();
            });
            return false;*/
            var zxywl=0;//咨询业务量
            var yjywl=0;//邮寄业务量
            var tr_data='';//行数据，多行用逗号分隔开
            $('#items_table tr').each(function(i){          // 遍历 tr
                if(i>=1){
                    console.info("第"+i+"行");
                    var id=0;
                    var xsywl=0;//线上业务量
                    var xxywl_dt=0;//线下业务量_大厅
                    var xsywl_ft=0;//线下业务量_分厅

                    id=$(this).children('td:eq(0)').children("input:eq(0)").val();
                    if(i==1){
                        zxywl=$(this).children('td:eq(5)').children("input:eq(0)").val();
                        yjywl=$(this).children('td:eq(6)').children("input:eq(0)").val();

                        xsywl=$(this).children('td:eq(7)').children("input:eq(0)").val();
                        xxywl_dt=$(this).children('td:eq(8)').children("input:eq(0)").val();
                        xsywl_ft=$(this).children('td:eq(9)').children("input:eq(0)").val();

                        tr_data+=id+'_'+xsywl+'_'+xxywl_dt+'_'+xsywl_ft;
                    }else{
                        xsywl=$(this).children('td:eq(5)').children("input:eq(0)").val();
                        xxywl_dt=$(this).children('td:eq(6)').children("input:eq(0)").val();
                        xsywl_ft=$(this).children('td:eq(7)').children("input:eq(0)").val();
                        tr_data+=','+id+'_'+xsywl+'_'+xxywl_dt+'_'+xsywl_ft;
                    }
                }
            });
            var pushObj={};
            pushObj.tr_data=tr_data;
            pushObj.answer_count=zxywl;
            pushObj.post_count=yjywl;
            pushObj.buss_time=$("#febs-form-group-date").val();
            febs.post(ctx + 'pushData', pushObj, function () {
                febs.alert.success('数据上传成功');
            });
        });

        function initTable(){
            var obj={};
            obj.buss_time=$("#febs-form-group-date").val();
            febs.get(ctx + 'hallItemData', obj, function (data) {
                if(data.code==200){
                    if(!data.isUpdate){
                        $("#submit").hide();
                    }else{
                        $("#submit").show();
                    }
                    var d=data.data;
                    var tbodyHtml='';
                    for(var i=0;i<d.length;i++){
                        tbodyHtml+='<tr>';
                        tbodyHtml+='<td>'+d[i].item.departName+'<input type="hidden" value="'+d[i].item.id+'" /></td>';
                        tbodyHtml+='<td>'+d[i].item.mainItemName+'</td>';
                        tbodyHtml+='<td>'+d[i].item.sumItemName+'</td>';
                        tbodyHtml+='<td>'+d[i].item.itemType+'</td>';
                        tbodyHtml+='<td>'+d[i].item.name+'</td>';
                        if(i==0){
                            tbodyHtml+='<td rowspan="'+d.length+'"><input type="text"  style="width:50px" value="'+d[i].hallUpData.seekBussCount+'"/></td>';
                            tbodyHtml+='<td rowspan="'+d.length+'"><input type="text" style="width:50px" value="'+d[i].hallUpData.mailBussCount+'"/></td>';
                        }
                        tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.onlineBussCount+'" style="width:50px"/></td>';
                        if(d[i].item.addressType=='大厅'){
                            tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.underlineHall+'" style="width:50px"/></td>';
                            tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.underlineSubHall+'" style="width:50px;border:1px solid red;" readonly="true"/></td>';
                        } else if(d[i].item.addressType=='分厅'){
                            tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.underlineHall+'" style="width:50px;border:1px solid red;" readonly="true"/></td>';
                            tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.underlineSubHall+'" style="width:50px"/></td>';
                        }else{
                            tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.underlineHall+'" style="width:50px"/></td>';
                            tbodyHtml+='<td ><input type="text" value="'+d[i].hallUpData.underlineSubHall+'" style="width:50px"/></td>';
                        }

                        tbodyHtml+='</tr>';
                    }
                    $("#items_table_tbody").html(tbodyHtml);
                }
            });
        }
    });
</script>